package cn.edu.dhu.acm.oj.client.panel;

import cn.edu.dhu.acm.oj.client.Control;
import cn.edu.dhu.acm.oj.common.config.Const;

public class PaperSelectPanel extends javax.swing.JPanel {

	class Item {

		String name;
		int index;

		Item(String str, int i) {
			name = str;
			index = i;
		}

		@Override
		public String toString() {
			return name;
		}

		public int getIndex() {
			return index;
		}

		public String getName() {
			return name;
		}
	}
	/** Creates new form PaperSelectPanel */
	private javax.swing.JDialog dialog;

	public PaperSelectPanel(javax.swing.JDialog d) {
		initComponents();
		dialog = d;
		if (Control.getModel().startsWith("Local")) {
			//Local model
			JCB_Paper.setEnabled(true);
			try {
				java.io.File dir = new java.io.File(Control.getDhuojhomepath());
				String[] files = dir.list();
				for (int i = 0; i < files.length; i++) {
					java.io.File file = new java.io.File(dir, files[i]);
					String name = file.getName();
					if (name.endsWith(Const.CLIENTPAPERSUFFIX) || name.endsWith(Const.ENCRYPTPAPERSUFFIX)) {
						JCB_Paper.addItem(file.getName());
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			JCB_Paper.addItem(Control.getContestTitle());
		}
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();
        JCB_Paper = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        JPF_Password = new javax.swing.JPasswordField();
        JB_Open = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setLayout(new java.awt.BorderLayout());

        jPanel1.setLayout(new java.awt.BorderLayout());
        add(jPanel1, java.awt.BorderLayout.NORTH);

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);
        jToolBar1.add(JCB_Paper);

        jLabel1.setText("Password:");
        jToolBar1.add(jLabel1);

        JPF_Password.setColumns(10);
        jToolBar1.add(JPF_Password);

        JB_Open.setText("Open");
        JB_Open.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JB_OpenActionPerformed(evt);
            }
        });
        jToolBar1.add(JB_Open);

        add(jToolBar1, java.awt.BorderLayout.NORTH);

        jLabel2.setText("If you enable visual effects on Linux, it may cause you unable to press Password, just close the effects ! ");
        add(jLabel2, java.awt.BorderLayout.SOUTH);

        jLabel3.setText("Most contests don't have Password, just Open it! For those that require password, please contact the admin.");
        add(jLabel3, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

	private void JB_OpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JB_OpenActionPerformed
		dialog.dispose();
		if (Control.getModel().startsWith("Local")) {
			Control.setPaper(Control.getDhuojhomepath() + JCB_Paper.getSelectedItem().toString(), JPF_Password.getText());
		} else {
			Control.setPaper("CONTEST", JPF_Password.getText());
		}
}//GEN-LAST:event_JB_OpenActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton JB_Open;
    private javax.swing.JComboBox JCB_Paper;
    private javax.swing.JPasswordField JPF_Password;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JToolBar jToolBar1;
    // End of variables declaration//GEN-END:variables
}
